package com.sunshine.lnuplus.view.Main;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import com.sunshine.lnuplus.service.LoadBackgroundService;
import com.sunshine.lnuplus.utils.TimeHelper;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class MainViewModel extends AndroidViewModel {
    private Application application;
    private SharedPreferences appSetting;
    private boolean everydayBackground,firstStart,checkUpdate;
    private SharedPreferences.Editor editor;

    public MainViewModel(@NonNull Application application) {
        super(application);
        this.application = application;
        appSetting = application.getSharedPreferences("app_setting",Context.MODE_PRIVATE);
        editor = appSetting.edit();
        everydayBackground = appSetting.getBoolean("everyday_background",true);
        firstStart = appSetting.getBoolean("first_start",true);
        checkUpdate = appSetting.getBoolean("check_update",true);
        if(everydayBackground) toGetBackground();
    }

    //判断是否需要从网络获取背景
    private void toGetBackground(){
        String backgroundFileName = application.getFilesDir().getAbsolutePath() + "/"+TimeHelper.getMonth()+"-"+ TimeHelper.getDay() +".jpg";
        try {
            FileInputStream todayBackground = new FileInputStream(backgroundFileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            application.startService(new Intent(application, LoadBackgroundService.class));
        }
    }

    boolean isEverydayBackground() {
        return appSetting.getBoolean("everyday_background",true);
    }

    boolean getFirstStart(){
        return firstStart;
    }

    boolean isCheckUpdate(){
        return checkUpdate;
    }

    void setStart(){
        editor.putBoolean("first_start",false);
        editor.apply();
    }
}
